package com.sanrenzu.everybodytv.http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sanrenzu.everybodytv.bean.gy.LiveBean;
import com.sanrenzu.everybodytv.bean.zxb.ColumItemBean;
import com.sanrenzu.everybodytv.bean.zxb.VideoItemBean;
import com.sanrenzu.everybodytv.bean.zxb.ZhiBoBean;
import com.sanrenzu.everybodytv.config.UrlConfig;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class HttpUtils {

    //保证http访问工具仅有一个
    private   static  volatile HttpUtils sinleton;
    private Retrofit retrofit;
    private IDataBean iDataBean;
    private HttpUtils() {
        retrofit = createRetrofit();
        iDataBean= createIDataBean();
    }

    private IDataBean createIDataBean() {
        IDataBean iDataBean =   retrofit.create(IDataBean.class);
        return iDataBean;
    }

    //同步锁定http访问类，保证每次仅一个，锁定后通过类的实例继续调用
    public static HttpUtils getInstance(){
        if(sinleton==null){
            synchronized (HttpUtils.class){
                if (sinleton==null){
                    sinleton = new HttpUtils();
                }
            }
        }
        return sinleton;
    }

    /**
     * Retrofit 实例化
     * @return
     */
    private Retrofit createRetrofit(){
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .build();
    }
    /**
     * Gson实例化
     * @return
     */
    private Gson createGson(){
        Gson gson = new GsonBuilder()
                //序列化不为空
                .serializeNulls()
                .create();
        return gson;
    }

    public void getVerticalColumItem(Callback<List<ColumItemBean>> columItemBeanCallback){
        Call<List<ColumItemBean>> call = iDataBean.getVerticalColumItem();
        call.enqueue(columItemBeanCallback);
    }

    public void getVerticalVideoItem(String name,Callback<VideoItemBean> videoItemBeanCallback){
        Call<VideoItemBean> call = iDataBean.getVerticalVideoItem(name);
        call.enqueue(videoItemBeanCallback);
    }

    public void getVerticalZhiBo(String name, Callback<ZhiBoBean> zhiBoBeanCallback){
        Call<ZhiBoBean> call = iDataBean.getVerticalZhiBo(name);
        call.enqueue(zhiBoBeanCallback);
    }
}
